package com.ly.mp.dal.comm.config;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

@Component
@Lazy(false)
@ConfigurationProperties(prefix = "read.jdbc", ignoreUnknownFields = true)
public class DataBaseReadSet {
    private Map<String, String> name;
    private Map<String, String> url;
    private Map<String, String> username;
    private Map<String, String> password;
    private Map<String, List<DataSourceInfo>> readDbInfos = new HashMap<>();
    private Map<String, Integer> readDbSizes = new HashMap<>();

    public void initReadDB(String defaultName) {
        if (this.name != null && !this.name.isEmpty()) {
            Iterator var2;
            Map.Entry entry;
            DataSourceInfo info;
            List infoList;
            for (var2 = this.name.entrySet().iterator(); var2.hasNext(); ((List) infoList).add(info)) {
                entry = (Map.Entry) var2.next();
                info = new DataSourceInfo();
                String key = (String) entry.getKey();
                info.setName((String) entry.getValue());
                info.setUrl(this.url.get(key));
                info.setUsername(this.username.get(key));
                info.setPassword(this.password.get(key));
                String readDbKey = key.split("#")[0];
                if (defaultName.equals(readDbKey)) {
                    readDbKey = "default";
                }

                infoList = this.readDbInfos.computeIfAbsent(readDbKey, k-> new ArrayList<DataSourceInfo>());
            }

            var2 = this.readDbInfos.entrySet().iterator();

            while (var2.hasNext()) {
                entry = (Map.Entry) var2.next();
                this.readDbSizes.put((String)entry.getKey(), ((List) entry.getValue()).size());
            }

        }
    }

    public int getReadDbSizes(String key) {
        Integer size = this.readDbSizes.get(key);
        size = size == null ? (Integer) this.readDbSizes.get("default") : size;
        return size == null ? 0 : size;
    }

    public Map<String, List<DataSourceInfo>> getReadDbInfos() {
        return this.readDbInfos;
    }

    public Map<String, String> getName() {
        return this.name;
    }

    public Map<String, String> getUrl() {
        return this.url;
    }

    public Map<String, String> getUsername() {
        return this.username;
    }

    public Map<String, String> getPassword() {
        return this.password;
    }

    public void setName(Map<String, String> name) {
        this.name = name;
    }

    public void setUrl(Map<String, String> url) {
        this.url = url;
    }

    public void setUsername(Map<String, String> username) {
        this.username = username;
    }

    public void setPassword(Map<String, String> password) {
        this.password = password;
    }
}